<?php

namespace App\Http\Resources\Article;

use App\Http\Resources\BaseResource;
use App\Models\Article\ArticleModel;
use App\Models\Article\ArticleUserRelationModel;
use Illuminate\Http\Resources\MissingValue;

class UserOperateListResource extends BaseResource
{
    public function toArray($request): array
    {
        return [
            "id"               => $this->id,
            "type"             => $this->type,
            "typeText"         => ArticleUserRelationModel::TYPE_TEXT[$this->type] ?? "-",
            "createTime"       => $this->createdAt->timestamp,
            "formatCreateTime" => formatDate($this->createdAt->timestamp, "-"),
            "articleId"        => $this->articleId,
            "article"          => $this->getArticleDetail()
        ]; // TODO: Change the autogenerated stub
    }

    /**
     * @return array|null
     */
    public function getArticleDetail(): ?array
    {
        if ($this->articleRel) {
            return [
                "title"            => $this->articleRel->title,
                "type"             => $this->articleRel->type,
                "previewUrl"       => json_decode($this->articleRel->previewUrl, true),
                "coverImgUrl"      => $this->articleRel->coverImgUrl,
                "description"      => $this->articleRel->description,
                "supportNum"       => $this->articleRel->supportNum,
                "collectNum"       => $this->articleRel->collectNum,
                "forwardNum"       => $this->articleRel->forwardNum,
                "readNum"          => $this->articleRel->readNum,
                "showNum"          => $this->articleRel->showNum,
//                "formatEffectiveTime" => formatDate($this->articleRel->effectiveTime),
//                "effectiveTime"       => $this->articleRel->effectiveTime,
//                "status"              => $this->articleRel->status,
//                "statusText"          => translateEnumCodeToCn(ArticleModel::STATUS_TEXT, $this->articleRel->status,
//                    "-"),
//                "publishType"         => $this->articleRel->publishType,
//                "publishTypeText"     => translateEnumCodeToCn(ArticleModel::PUBLISH_TYPE_TEXT,
//                    $this->articleRel->publishType,
//                    "-"),
                "createTime"       => $this->articleRel->createdAt->timestamp,
                "formatCreateTime" => formatDate($this->articleRel->createdAt->timestamp, "-"),
                "sort"             => $this->articleRel->sort,
                "putTop"           => $this->articleRel->putTop,
                "extra"            => $this->articleRel->extra,
                "isDeleted"        => $this->isDeleted
            ];
        } else {
            return null;
        }
    }
}
